home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / blckade.exe / DEMO.C < prev    next >
C/C++ Source or Header  |  1992-08-23  |  6KB  |  188 lines

  1.  
  2. /* -------------------------------------------------------------- */
  3. /*            Blockade(tm) demonstration program.                 */
  4. /*                                                                */
  5. /*  Demonstrates the use of the blockade self checking routines.  */
  6. /*                                                                */
  7. /*   2.05 - updated to give all programs same rev. level.         */
  8. /*          added isprint() check for user messgae.               */
  9. /*   2.04 - updated to show use of user data area.                */
  10. /*   1.03 - automatically clear patch area if /O specified.       */
  11. /*   1.02 - added more comments and cleaned up a little.          */
  12. /*   1.01 - added patch overide /O function to accomodate MSC     */
  13. /*          which places a value in the exe header chksum.        */
  14. /*                                                                */
  15. /* -------------------------------------------------------------- */
  16.  
  17. #include "allstds.h"        /* Standard stuff  */
  18. #ifdef __TURBOC__           /* defined in BC++ */
  19.  #pragma hdrstop
  20. #endif
  21.  
  22. #include "blkproto.h"       /* function prototypes for the library */
  23. #include "demosig.h"        /* holds the signature string          */
  24.  
  25. /* ------------------------------------------- */
  26.  
  27. int main( int argc,char * *argv );
  28. void do_args( int, char **);
  29. int patch_checksum( char *, unsigned );
  30.  
  31. /* ------------------------------------------- */
  32.  
  33. int fix_mode = 0;              /* 1 = patch to fix...        */
  34. int corrupt_mode = 0;          /* 1 = Corrupt to force error */
  35. unsigned fix_value;            /* value to use to patch      */
  36.  
  37. #ifdef __TURBOC__
  38. int patch_overide = 0;         /* There shouldn't be any value.     */
  39. #else
  40. int patch_overide = 1;         /* But MSC uses the checksum area ?! */
  41. #endif
  42.  
  43. /* ------------------------------------------- */
  44.  
  45. main(int argc,char **argv)
  46. {
  47.  int blkstat, usrsize;
  48.  char *sp;
  49.  
  50.  printf("\n");
  51.  printf("BLOCKADE (tm) demo program (%s). Ver. 2.05 \n", argv[0]);
  52.  printf("   Copyright (C) 1991-92 Indusoft Corp. \n\n");
  53.  printf(" Switches:\n");
  54.  printf("    /C  - corrupt myself so self check fails.\n");
  55.  printf("    /F  - fix myself so self check passes.\n\n");
  56.  
  57.  do_args( argc, argv);
  58.  
  59.  if (fix_mode || corrupt_mode)
  60.         { patch_checksum( argv[0], fix_value);  }
  61.  
  62.  printf("Checking myself for changes..\n");
  63.  
  64.  blkstat = blockade( argv[0], 780);       /* the heart of the demo.. */
  65.  
  66.  printf ("Results of file self check = %d\n", blkstat);
  67.  
  68.  sp = get_blk_err_msg( blkstat);
  69.  printf("Status message from file self check was \"%s\". \n", sp);
  70.  if (blkstat == 7)
  71.    {
  72.     printf("Run \"Brand DEMO  /us=HI! \" "
  73.            "or \"Brand DEMO /uf=USERDATA.DAT\"\n");
  74.     printf("to initialize this file and retry..\n");
  75.    }
  76.  
  77. /*   Show how the user function works if anything is there       */
  78. /*   While the user data does not have to be a string (it could  */
  79. /*   be binary from a file), don't print it if it is not.        */
  80.  
  81.  sp = (char *) blk_user_pointer(&usrsize);
  82.  if (strlen(sp) && (sp != NULL) && isprint(*sp) && (blkstat == 0))
  83.     {
  84.         printf("User data : %s \n",sp);
  85.     }
  86.  
  87.  return(0);
  88.  }
  89.  
  90. /*-------------------------------------------------------------------*/
  91. /*  Gathers up the command line switches.                            */
  92. /*                                                                   */
  93. /*-------------------------------------------------------------------*/
  94.  
  95. void do_args( int argc, char **argv)
  96.  {
  97.    char buf[8], *sp;
  98.    int j;
  99.  
  100.   for (j = 1; j < argc; j++)
  101.     {
  102.       sp = buf;
  103.       strncpy( buf, argv[j], sizeof(buf)-1);
  104.       strupr( buf );
  105.       if (*sp =='-') *sp = '\\';       /* accommodate those unix guys..*/
  106.       if (*sp =='/') sp++;             /* look to next char...         */
  107.       else           continue;         /* nothing I know how to handle */
  108.  
  109.       if (*sp == 'F')
  110.            {  fix_mode = 1;     fix_value = 0;  }
  111.       if (*sp == 'C')
  112.            {  corrupt_mode = 1; fix_value = 1;  }
  113.       if (*sp == 'O')
  114.            {  patch_overide = 1;
  115.               fix_mode = 1;     fix_value = 0;  }
  116.    }
  117.  }
  118.  
  119. /* -------------------------------------------------------------- */
  120.  
  121. #pragma pack(1)                /* MSC only, ignored by TC/BC */
  122.  
  123. typedef struct exeheadr {       /* Structure of .EXE file header */
  124.        unsigned char sig[2];
  125.        unsigned mod512;
  126.        unsigned num_pages;
  127.        unsigned reloc_cnt;
  128.        unsigned hdr_size;
  129.        unsigned min_para;
  130.        unsigned max_para;
  131.        unsigned stack_seg;
  132.        unsigned start_sp;
  133.        unsigned checksum;        /* here's where we patch... */
  134.        unsigned entry_ip;
  135.        unsigned code_seg;
  136.        unsigned relo_offset;
  137.        unsigned short    ovl_num;
  138.        unsigned short    relo_stuff[80];
  139.        } EXEHDR;
  140.  
  141.  EXEHDR   exehdr;
  142.  
  143. /* --------------------------------------------------------------- */
  144.  
  145. int patch_checksum( char *infile, unsigned value)
  146. {
  147.  size_t stat, check, ok_to_patch = 1;
  148.  FILE *fd;
  149.  
  150.  if (fix_mode)      printf("Zeroing DEMO file patch area...\n");
  151.  if (corrupt_mode)  printf("Corrupting DEMO file...\n");
  152.  
  153.  if ((fd = fopen(infile,"r+b")) == NULL)
  154.   {
  155.      fprintf(stderr,"Unable to open %s .\n",infile);
  156.      return(2);
  157.   }
  158.  
  159.   stat = fread( (void *)&exehdr, 1, sizeof(exehdr), fd);
  160.   if (stat != sizeof(exehdr))
  161.       {  printf ("Error reading exe header of demo file\n");     }
  162.  
  163.   if ((exehdr.sig[0] != 'M') || (exehdr.sig[1] != 'Z'))
  164.       {
  165.          printf ("Error, not a valid EXE file signature.\n");
  166.       }
  167.  
  168.   check = exehdr.checksum;
  169.   if (check != 0 && check != 1)
  170.     {
  171.       printf("Warning unexpected value (%u) found in Demo patch area.\n",check);
  172.       if (!patch_overide) ok_to_patch = 0;
  173.     }
  174.  
  175.   if (ok_to_patch)
  176.            exehdr.checksum = value;
  177.  
  178.   rewind(fd);
  179.   stat = fwrite( (void *)&exehdr, 1, sizeof(exehdr), fd);
  180.   if (stat != sizeof(exehdr))
  181.       {  printf ("Error writing to demo file. \n");     }
  182.  
  183.   fclose(fd);
  184.   return(0);
  185. }
  186.  
  187. /* ------------------------------------------------------ */
  188.